home *** CD-ROM | disk | FTP | other *** search
/ Linux Cubed Series 2: Applications / Linux Cubed Series 2 - Applications.iso / circuits / irsim-9.000 / irsim-9 / src / irsim / access.c next >
C/C++ Source or Header  |  1993-01-15  |  2KB  |  77 lines

  1. /* 
  2.  *     ********************************************************************* 
  3.  *     * Copyright (C) 1988, 1990 Stanford University.                     * 
  4.  *     * Permission to use, copy, modify, and distribute this              * 
  5.  *     * software and its documentation for any purpose and without        * 
  6.  *     * fee is hereby granted, provided that the above copyright          * 
  7.  *     * notice appear in all copies.  Stanford University                 * 
  8.  *     * makes no representations about the suitability of this            * 
  9.  *     * software for any purpose.  It is provided "as is" without         * 
  10.  *     * express or implied warranty.  Export of this software outside     * 
  11.  *     * of the United States of America may require an export license.    * 
  12.  *     ********************************************************************* 
  13.  */
  14.  
  15. /*
  16.  * front-end to the more general 'access' facility.
  17.  */
  18.  
  19. #ifdef SYS_V
  20. #include <sys/types.h>
  21. #include <unistd.h>
  22. #endif
  23. #include <sys/file.h>
  24. #include "defs.h"
  25.  
  26.  
  27. public    typedef    struct
  28.   {
  29.     char  exist;
  30.     char  read;
  31.     char  write;
  32.   } Fstat;
  33.  
  34.  
  35. public Fstat *FileStatus( name )
  36.   char   *name;
  37.   {
  38.     static Fstat   ret;
  39.     char           dir[ 256 ];
  40.     register char  *s, *q, *p;
  41.     
  42.     if( access( name, R_OK ) == 0 )
  43.     ret.read = 1;                /* file exists and is readable */
  44.     else
  45.     ret.read = 0;
  46.  
  47.     if( access( name, W_OK ) == 0 )    /* file exists and is writeable */
  48.       {
  49.     ret.exist = 1;
  50.     ret.write = 1;
  51.     return( &ret );
  52.       }
  53.  
  54.     if( access( name, F_OK ) == 0 )    /* file exists but isn't writeable */
  55.       {
  56.     ret.exist = 1;
  57.     ret.write = 0;
  58.     return( &ret );
  59.       }
  60.                     /* file doesn't exist, check dir. */
  61.     p = name;
  62.     for( s = p; *s != '\0'; s++ );
  63.     while( s > p and *s != '/' ) s--;
  64.     if( *s == '/' ) s++;
  65.     q = dir;
  66.     while( p < s )
  67.     *q++ = *p++;
  68.     *q++ = '.';
  69.     *q = '\0';
  70.     if( access( dir, W_OK ) == 0 )
  71.     ret.write = 1;                /*  dir. is writeable */
  72.     else
  73.     ret.write = 0;
  74.     ret.exist = 0;
  75.     return( &ret );
  76.   }
  77.